var vm = new Vue({
    el: '#rrapp',
    data: {
        resultBrandName: '',
        resultShow: false,
        showList: 1,
        showAddTemp: false,
        showAddType: false,
        title: null,
        bArticleTemp: {},
        bArticleTempType: {},
        bArticleTempTypeLists: [],
        isA: -1,
        isT: "",
        arrayOld: [],
        arrayNew: [],
        arrayCreat: [],
        modelShow: false,
        modelMsg: "",
    },
    mounted() {
        var vm = this;
        console.info(vm.showList);

        vm.reloadType();
        // this.init();
    },
    methods: {
        clickActive: function (index) {
            if (vm.isA == index) {
                vm.isA = -1;
                return;
            }

            vm.isA = index;
            // if (vm.isA != -1) {
            vm.reload();
            // }
        },
        query: function () {
            console.info(vm.isA);
            console.info(vm.isT);
            vm.reload();
        },
        add: function () {
            vm.showList = 2;

            vm.title = "新增文章模板";
            vm.bArticleTemp = {
                status: 1,
                articleTypeId: -1,
            };
            $('input[type=file]').val("");
            window.current_editor.setContent("");
        },
        addType: function () {
            vm.showList = 3;

            vm.title = "新增分类";
            vm.bArticleTempType = {
                status: 1,
            };
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = 2;

            vm.title = "修改文章模板";
            $('input[type=file]').val("");
            window.current_editor.setContent("");
            vm.getInfo(id)
        },
        updateById: function (id) {
            if (id == null) {
                return;
            }
            vm.showList = 2;

            vm.title = "修改文章模板";

            vm.getInfo(id)
        },
        status: function (e, x) {
            let bArticleTemp = {
                id: e,
                status: x,
            };
            let text = "删除";
            if (x == 1) {
                text = "恢复"
            }
            confirm('确定要' + text + '选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/barticletemp/updateStatus",
                    contentType: "application/json;charset=UTF-8",
                    data: JSON.stringify(bArticleTemp),
                    success: function (r) {
                        if (r.code === 0) {
                            alert('操作成功', function (index) {
                                vm.reload();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        updateType: function (event) {
            let id = vm.isA;
            if (id == null || id == -1) {
                alert("请先选择分类！");
                return;
            }

            vm.showList = 3;
            vm.title = "修改分类";
            console.info(vm.isA);
            // id = vm.bArticleTempTypeLists[id].id;

            vm.getInfoType(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.bArticleTemp.id == null ? "sys/barticletemp/save" : "sys/barticletemp/update";

            vm.bArticleTemp.articleTitle = $("#articleTitle").val();
            vm.bArticleTemp.articleDesc = $("#articleDesc").val();
            vm.bArticleTemp.articleContent = window.current_editor.getContent();

            if (vm.bArticleTemp.orderNum < -2147483648 || vm.bArticleTemp.orderNum > 2147483647) {
                alert("排序参数应在 (-2147483648 - 2147483647) 范围内！");
                return;
            }

            if (vm.bArticleTemp.articleTitle == null || vm.bArticleTemp.articleTitle == "") {
                alert("文章标题不能为空！");
                return;
            }

            if (vm.bArticleTemp.articleTypeId == null || vm.bArticleTemp.articleTypeId == -1) {
                alert("文章类型没有选择！");
                return;
            }

            if (vm.bArticleTemp.articleDesc == null || vm.bArticleTemp.articleDesc == "") {
                alert("文章配文不能为空！");
                return;
            }

            if (vm.bArticleTemp.articleContent == null || vm.bArticleTemp.articleContent == "") {
                alert("文章内容不能为空！");
                return;
            }

            let loading = layer.msg('模板文章保存中，别催我...', {icon: 16, shade: 0.3, time: 0})
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json;charset=UTF-8",
                data: JSON.stringify(vm.bArticleTemp),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                    layer.close(loading);
                }
            });
        },
        saveOrUpdateType: function (event) {
            let url = vm.bArticleTempType.id == null ? "sys/barticletemptype/save" : "sys/barticletemptype/update";

            if (vm.bArticleTempType.typeName == null || vm.bArticleTempType.typeName.trim() == "") {
                alert("分类名称不能为空！");
                return;
            }

            if (vm.bArticleTempType.orderNum < -2147483648 || vm.bArticleTempType.orderNum > 2147483647) {
                alert("排序参数应在 (-2147483648 - 2147483647) 范围内！");
                return;
            }

            let loading = layer.msg('模板分类保存中，别催我...', {icon: 16, shade: 0.3, time: 0})
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.bArticleTempType),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.closeType();
                        });
                    } else {
                        alert(r.msg);
                    }
                    layer.close(loading);
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的模板文章？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/barticletemp/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        delType: function (event) {
            let id = vm.isA;

            if (id == null || id == -1) {
                alert("请先选择分类！");
                return;
            }

            // id = vm.bArticleTempTypeLists[id].id;

            let ids = new Array();
            ids.push(id)

            confirm('确定要删除选中的分类？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/barticletemptype/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                vm.reloadType();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "sys/barticletemp/info/" + id, function (r) {
                vm.bArticleTemp = r.bArticleTemp;
                if (r.bArticleTemp.articleTypeId == null || r.bArticleTemp.articleTypeId == 0) {
                    vm.bArticleTemp.articleTypeId = -1
                }
                ;
                window.current_editor.setContent(r.bArticleTemp.articleContent);
            });
        },
        getInfoType: function (id) {
            $.get(baseURL + "sys/barticletemptype/info/" + id, function (r) {
                vm.bArticleTempType = r.bArticleTempType;
            });
        },
        reload: function (event) {
            vm.showList = 1;

            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                page: page,
                postData: {"articleTypeId": vm.isA, "articleTitle": vm.isT}
            }).trigger("reloadGrid");

            vm.reloadType();
        },
        closeType: function (event) {
            vm.showList = 1;
            vm.isA = -1;
            vm.reloadType();
        },
        reloadType: function (event) {
            $.get(baseURL + "sys/barticletemptype/lists", function (r) {
                vm.bArticleTempTypeLists = r.bArticleTempTypeLists;
            });
        },
        reloadCreat: function (event) {

            let loading = layer.msg('查询中，别催我...', {icon: 16, shade: 0.3, time: 0})
            let info = {
                articleTypeId: vm.isA,
                articleTitle: vm.isT,
            };

            $.ajax({
                type: "POST",
                url: baseURL + "sys/barticletemp/lists",
                contentType: "application/json",
                data: JSON.stringify(info),
                success: function (r) {
                    if (r.code === 0) {
                        vm.arrayOld = r.bArticleTempLists;
                        // vm.arrayNew = [];
                    } else {
                        alert(r.msg);
                    }
                    layer.close(loading);
                }
            });
        },
        delOld: function (item) {
            let has = false;
            for (let val of vm.arrayNew) {
                if (val.id == vm.arrayOld[item].id) {
                    has = true;
                }
            }

            if (!has) {
                vm.arrayNew.push(vm.arrayOld[item]);
                vm.arrayNew = vm.arrayNew.sort(vm.compare('id'));
            }

            // vm.arrayOld.splice(item, 1);
            console.log(vm.arrayOld.length)
        },
        delNew: function (item) {
            // vm.arrayOld.push(vm.arrayNew[item]);
            // vm.arrayOld = vm.arrayOld.sort(vm.compare('id'));

            vm.arrayNew.splice(item, 1);
            console.log(vm.arrayNew.length)
        },
        compare: function (property) {
            return function (a, b) {
                let value1 = a[property];
                let value2 = b[property];
                return value1 - value2;
            }
        },
        setShowList: function (number) {
            if (number == 2 && vm.arrayNew.length == 0) {
                alert('亲，您还没有选模板文章！', function (index) {
                    vm.showList = 1;
                });
                return;
            }
            vm.showList = number;
        },
        creatArticle: function () {
            if (vm.showList == 2 && vm.arrayNew.length == 0) {
                alert('亲，您还没有选模板文章！', function (index) {
                    vm.showList = 1;
                });
                return;
            }

            let brandName = $("#brandNames").val();
            if (brandName == null || brandName.trim().length == 0) {
                alert('亲，您还没有选目标品牌！', function (index) {

                });
                return;
            }

            let loading = layer.msg('文章拼命生成中，别催我...', {icon: 16, shade: 0.3, time: 0})
            let bArticleTempIds = [];

            for (let val of vm.arrayNew) {
                bArticleTempIds.push(val.id)
            }
            ;

            let bArticleTemp = {
                brandName: brandName,
                ids: bArticleTempIds,
            };

            console.log(bArticleTemp)
            $.ajax({
                type: "POST",
                url: baseURL + "sys/barticletemp/creatArticle",
                contentType: "application/json",
                data: JSON.stringify(bArticleTemp),
                success: function (r) {
                    if (r.code === 0) {
                        alert('入库成功！', function (index) {
                            vm.resultShow = true;
                            vm.arrayCreat = r.bArticleInfoList;
                            vm.resultBrandName = brandName;
                            console.log(r.bArticleInfoList)
                            console.log(r.bArticleBrandName)
                        });
                    } else {
                        alert(r.msg);
                    }
                    layer.close(loading);
                }
            });

        },
        gotoArticleInfo: function () {
            $('.fa.fa-window-restore', parent.document).click();
            // window.open("../sys/barticleinfo.html");
        },
        openModel: function (ids) {
            for (let val of vm.arrayOld) {
                if (val.id == ids) {
                    $.get(baseURL + "sys/barticletemp/info/" + ids, function (r) {
                        vm.modelShow = true;
                        vm.bArticleTemp = r.bArticleTemp;
                        if (r.bArticleTemp.articleTypeId == null || r.bArticleTemp.articleTypeId == 0) {
                            vm.bArticleTemp.articleTypeId = -1
                        }
                        ;
                        vm.modelMsg = r.bArticleTemp.articleContent;
                    });
                }
            }
        },
        closeModel: function (ids) {
            vm.modelShow = false;
            vm.modelMsg = "";
        }
    }
});
